using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;

namespace Accountability4net
{
    /// <summary>
    /// Reads the application configuration and instantiates a provider.
    /// </summary>
    /// <seealso cref="MonengineProviderBase"/>
    /// <seealso cref="ProviderConfig"/>
    public class MonengineManager
    {
        static MonengineProviderBase provider_;
        /// <summary>
        /// The provider instance that works as the central point of getting and creating
        /// implementors of the interfaces. Only one provider can be active for the application
        /// at the same time.
        /// </summary>
        public static MonengineProviderBase Provider
        {
            get
            {
                if(provider_ == null)
                {
                    ProviderSettingsCollection providerSettings = ProviderConfig.GetProviderConfig().Providers;
                    ProviderSettings providerSetting = providerSettings[0];
                    provider_ = (MonengineProviderBase)Activator.CreateInstance(Type.GetType(providerSetting.Type));
                    provider_.Initialize(providerSetting.Name, providerSetting.Parameters);
                }
                return provider_;
            }
            set
            {
                provider_ = value;
            }
        }

        /// <summary>
        /// Gets provider by name
        /// </summary>
        /// <param name="name">Name of the provider in the configuration file</param>
        /// <returns>The provider with <paramref name="name"/></returns>
        public static MonengineProviderBase GetProvider(string name)
        {
            ProviderSettingsCollection providerSettings = ProviderConfig.GetProviderConfig().Providers;
            ProviderSettings providerSetting = providerSettings[name];
            provider_ = (MonengineProviderBase)Activator.CreateInstance(Type.GetType(providerSetting.Type));
            provider_.Initialize(providerSetting.Name, providerSetting.Parameters);
            return provider_;
        }

        private static IList<MonengineProviderBase> providers_;
        public static IList<MonengineProviderBase> Providers
        {
            get
            {
                if (providers_ == null)
                {
                    ProviderSettingsCollection providerSettings = ProviderConfig.GetProviderConfig().Providers;
                    providers_ = new List<MonengineProviderBase>(providerSettings.Count);
                    foreach (ProviderSettings providerSetting in providerSettings)
                    {
                        MonengineProviderBase provider = (MonengineProviderBase)Activator.CreateInstance(Type.GetType(providerSetting.Type));
                        provider.Initialize(providerSetting.Name, providerSetting.Parameters);
                        providers_.Add(provider);
                    }
                }
                return providers_;
            }
        }
    }
}
